package com.yxh.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.annotation.SuppressLint;

public class DateUtil {
	/**
	 * 时间戳换算xxx前 **
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getTimeGap3(String beg, Date now) {
		try {
			SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String beginDate = TimeStamp3Date(beg);
			Date begin = dfs.parse(beginDate);
			// System.out.println("beginDate:"+ beginDate);
			// System.out.println("begin:"+ begin);
			String endData = dfs.format(now);
			Date end = dfs.parse(endData);
			long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
			long second = between;
			if (second < 60) {
				return "1分钟前";
			} else if (second < (60 * 60)) {
				long minute = between / 60;
				return minute + "分钟前";
			} else if (second < (24 * 60 * 60)) {
				long hour = between / 3600;
				return hour + "小时前";
			} else if (second < (30 * 24 * 60 * 60)) {
				long day = between / (24 * 3600);
				return day + "天前";
			} else if (second < (12 * 30 * 24 * 60 * 60)) {
				long month = between / (30 * 24 * 3600);
				return month + "月前";
			} else {
				long year = between / (12 * 30 * 24 * 3600);
				return year + "年前";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getCurrentTime() {
		String returnStr = null;
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		returnStr = f.format(date);
		return returnStr;
	}

	public static String getTime(long time) {
		Date currentTime = new Date(time);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static String getDataString(int pos) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, pos);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(date);
	}

	public static String date2TiemStamp(String dateStr) {
		Date date = new Date();
		// 注意format的格式要与日期String的格式相匹配
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return String.valueOf(date.getTime() / 1000);
	}
	
	
	public static long date2TiemStamp2(String dateStr) {
		Date date = new Date();
		// 注意format的格式要与日期String的格式相匹配
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime() / 1000;
	}

	public static String getTimeStamp(int pos) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, pos);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		return String.valueOf(date.getTime() / 1000);
	}

	public static String getTime2(long time) {
		Date currentTime = new Date(time);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static String TimeStamp3Date(String timeString) {
		Long timestamp = Long.parseLong(timeString) * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(new Date(timestamp));
		return date;
	}

	public static String TimeStamp3Date2(String timeString) {
		Long timestamp = Long.parseLong(timeString) * 1000;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String date = sdf.format(new Date(timestamp));
		return date;
	}


}
